# example6_3.py
# coding=utf-8
import numpy as np
import matplotlib.pyplot as plt

open_price, close_price = np.loadtxt(
    "./stock.csv", delimiter=",", usecols=(1, 4), unpack=True, skiprows=1
)

x = np.arange(len(close_price))  # 横坐标值
print(x)
# 方式1
# (plot1,) = plt.plot(x, open_price, "g--", marker="o", linewidth=1)  # 开盘价
# (plot2,) = plt.plot(x, close_price, "r", marker="+", linewidth=2)  # 收盘价
# 方式2
# plot1 = plt.plot(x, open_price, "g--", marker="o", linewidth=1)  # 开盘价
# plot2 = plt.plot(x, close_price, "r", marker="+", linewidth=2)  # 收盘价
# 方式3 label是用来显示legned用的
(plot1) = plt.plot(x, open_price, "g--", marker="o", linewidth=1, label="开盘价")
(plot2) = plt.plot(x, close_price, "r", marker="+", linewidth=2, label="收盘价")

# 为了显示中文，指定默认字体
plt.rcParams["font.sans-serif"] = ["SimHei"]

# plt.title("开盘价与收盘价历史走势图", fontsize=18)  # 添加图标题
# # 设置刻度字体大小
# plt.xticks(fontsize=15)
# plt.yticks(fontsize=15)
# plt.xlabel("Time", fontsize=15)  # 添加坐标轴标题
# plt.ylabel("Open and Close Price", fontsize=15)
# plt.xlim(0.0, max(x) + 1)  # 设定坐标轴限制
# plt.ylim(
#     min(min(open_price), min(close_price)) - 1,
#     max(max(open_price), max(close_price)) + 1,
# )

# 添加图例,方式1
# plt.legend(
#     (plot1, plot2), ("开盘价", "收盘价"), loc="lower right", fontsize=15, numpoints=3
# )
# plt.legend(
#     (plot1, plot2), ("开盘价", "收盘价"), loc=(0.05, 0.8), fontsize=15, numpoints=3
# )
# 添加图例,方式2
plt.legend(
    (plot1[0], plot2[0]),
    ("开盘价", "收盘价"),
    loc="lower right",
    fontsize=15,
    numpoints=3,
)
# 添加图例,方式3
# plt.legend(loc="lower right", fontsize=15, numpoints=3)

plt.show()
